package com.aidex.quartz.task;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.domain.SearchInfo;
import com.aidex.biz.service.*;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Autowired
    private SearchInfoService searchInfoService;
    @Autowired
    private ReptileService reptileService;
    @Autowired
    private SearchInfoDetailsService searchInfoDetailsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private TempShareService tempShareService;

    public String ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
        return "sdfsdfsdf";
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 京东爬虫
     *
     * @throws IOException
     * @throws InterruptedException
     */
    public void taskReptileJd() throws IOException, InterruptedException {
        SearchInfo searchInfo = new SearchInfo();
        searchInfo.setStatus("0");
        searchInfo = searchInfoService.findOne(searchInfo);
        if (null == searchInfo) {
            return;
        }
        String keyword = searchInfo.getName() + searchInfo.getBrandName();
        List<String> stringList = reptileService.runReptileByJd(keyword);
//        for (String url : stringList) {
//            SearchInfoDetails searchInfoDetails = new SearchInfoDetails();
//            searchInfoDetails.setBrandName(searchInfo.getBrandName());
//            searchInfoDetails.setName(searchInfo.getName());
//            searchInfoDetails.setUnit(searchInfo.getUnit());
//            searchInfoDetails.setInfoId(searchInfo.getId());
//            searchInfoDetails.setUrl("https://" + url);
//            searchInfoDetailsService.save(searchInfoDetails);
//        }
        if (stringList.size() > 0) {
            searchInfo.setDetailsId("https://" + stringList.get(0));
        }
        searchInfo.setStatus("2");
        searchInfoService.save(searchInfo);
    }


    /**
     * 用户分配
     */
    public void taskCustomerShare() {
        Customer customer = customerService.findOne();
        if (customer != null) {
            Map<String, Object> params = new HashMap<>();
            if (!StringUtils.isEmpty(customer.getCityName())) {
                String[] area = customer.getCityName().split("-");
                if (area.length == 2) {
                    params.put("cityName", area[1]);
                    params.put("type", customer.getType());
                    List<SysUser> sysUserList = iSysUserService.findShareUser(params);
                    if (null == sysUserList || sysUserList.size() == 0) {
                        //没有可分配的用户了  表示一轮已经分完了
                        SysUser sysUser=new SysUser();
                        sysUser.setCity(area[1]);
                        List<SysUser> shareUserList=iSysUserService.findList(sysUser);
                        if(shareUserList.size()>0){
                            tempShareService.deleteAll(shareUserList);
                        }
                        //重新查询
                        sysUserList = iSysUserService.findShareUser(params);
                    }
                    //如果查询到用户列表取第一个用户 如果没有用户了，返回空用户
                    SysUser user= sysUserList.size() > 0 ? sysUserList.get(0) : null;
                    customerService.shareCustomer(customer,user);
                }
            }


        }
    }

    /**
     * 每天 凌晨 重置日分配数量
     */
    public void taskResetDayShareCount(){
        iSysUserService.resetDayShareCount();
    }
}
